package com.soft.sitegenerator.business.hibernate;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import com.soft.sitegenerator.dao.HibernateDAO;
import com.soft.sitegenerator.exceptions.ObjectNotFoundException;

public class NewsGroup {

	private Long id;

	private String name;

	private String description;

	private Date creationDate;

	private User user;

	private Set users = new HashSet();

	private Set publishedPost = new HashSet();

	private HibernateDAO hibernateDAOHib;

	public Set getPublishedPost() {
		return publishedPost;
	}

	public void setPublishedPost(Set publishedPost) {
		this.publishedPost = publishedPost;
	}

	public Set getUsers() {
		return users;
	}

	public void setUsers(Set users) {
		this.users = users;
	}

	public Date getCreationDate() {
		return creationDate;
	}

	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}
	
	public String getFecha() {
		SimpleDateFormat formatoDelTexto = new SimpleDateFormat("dd/MM/yyyy");
		return(formatoDelTexto.format(this.getCreationDate()));
	}


	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	// HIBERNATE DAO
	public HibernateDAO getHibernateDAOHib() {
		return hibernateDAOHib;
	}

	public void setHibernateDAOHib(HibernateDAO hibernateDAOHib) {
		this.hibernateDAOHib = hibernateDAOHib;
	}

	public void load() throws ObjectNotFoundException {
		hibernateDAOHib.load(this, this.id);
	}

	public void save() {
		hibernateDAOHib.save(this);
	}

	public void update() {
		hibernateDAOHib.update(this);
	}

	public void delete() {
		hibernateDAOHib.delete(this);
	}

	public boolean exist() {
		try {
			hibernateDAOHib.exist(this, this.id);
		} catch (ObjectNotFoundException e) {
			return false;
		}
		return true;
	}

	public Collection findAllByUser(Long idUser) {
		return hibernateDAOHib.find("from NewsGroup where id_user = "+idUser+" order by name" );
	}
	public Collection findAllNotExist(Long idUser, Long idGroup) {
		return hibernateDAOHib.find("from NewsGroup where id_user <>"+idUser+" and id="+idGroup+" order by name" );
	}
	public Collection findAll() {
		return hibernateDAOHib.find("from NewsGroup order by name" );
	}
}
